package com.oceansoft.mobile.econsole.modules.api.controller;

import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import com.oceansoft.mobile.econsole.modules.base.entity.ApiPager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.weixin.entity.LostProperty;
import com.oceansoft.mobile.econsole.modules.weixin.service.ILostPropertyService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * 查询办事
 *
 * @author jin.xu
 * @createTime 2015年4月13日 下午2:18:53
 * @packagePath com.oceansoft.mobile.econsole.modules.weixin.controller.MatterQueryController.java
 */
@Controller
@RequestMapping("api/lost")
public class LostPropertyController extends BaseController {
    @Resource
    private ILostPropertyService lostPropertyService;

    /**
     * 查询失物寻物首页
     */
    @RequestMapping(value = "index")
    public String gotoLostPropertyIndex(Query query, Model model) {
        model.addAttribute("pager", lostPropertyService.loadLostProperties(query));
        return "weixin/searchLost/lost_found";
    }

    @RequestMapping(value = "details/{guid}", method = GET)
    public String gotoDetails(@PathVariable("guid") String guid, Model model) {
        Result result = lostPropertyService.findLostPropertyById(guid);
        if (result.isSucc()) {
            LostProperty lostProperty = (LostProperty) result.getData();
            model.addAttribute("lostProperty", lostProperty);
            return "weixin/searchLost/lost_details";
        }
        model.addAttribute("lostProperty", null);
        return "weixin/searchLost/lost_details";
    }

    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public Result loadLostPropertyByPage(Query query) {
        Result result = new Result();
        ApiPager<LostProperty> returnPage = lostPropertyService.loadLostProperties(query);
        result.setSucc(Result.SUCC);
        result.setData(returnPage);
        result.setMsg("");
        result.setHasNext(returnPage.isHasNext());
        return result;
    }

}

